package com.qf.openapi.web.master.filter;

import com.qf.openapi.web.master.utils.Contants;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.io.IOException;

@WebFilter("/logout")
@Slf4j
public class LogoutFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("*******LooutFilter初始化成功********************");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession();
        session.setAttribute(Contants.USER_TOKEN,null);
        log.debug("删除浏览器中的cokkie的usertoken");
        Cookie cookie = new Cookie(Contants.USER_TOKEN, "usertoken");
        cookie.setPath("/");
        cookie.setMaxAge(0);
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //注xiao当前用户
        log.debug("删除session中的user对象");
        response.addCookie(cookie);

        response.sendRedirect("/login.html");
    }

    @Override
    public void destroy() {
        log.debug("*******LooutFilter销毁成功********************");
    }
}
